package com.example.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.mapper.EmployeeMapper;
import com.example.demo.pojo.Employee;
import com.example.demo.service.IEmployeeService;
import com.example.demo.util.ServiceException;
import com.example.demo.vo.ResultVO;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author sssl
 * @since 2022-03-18
 */
@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements IEmployeeService {

    @Autowired
    EmployeeMapper employeeMapper;

    @Override
    public ResultVO selectSalePersonPage(Page<Employee> page, String name, String phone) {
        LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper<>();
        lqw.like(Strings.isNotEmpty(name), Employee::getName, name);
        lqw.like(Strings.isNotEmpty(phone), Employee::getPhone, phone);
        Page<Employee> employeePage = employeeMapper.selectPage(page, lqw);
        ResultVO vo = new ResultVO();

        vo.setCount(employeePage.getTotal());
        vo.setCode(0);
        vo.setMsg("");
        vo.setData(employeePage.getRecords());
        return vo;
    }

    @Override
    public Employee getUserByUserName(String username) {
        if (StringUtils.hasText(username)) {
            //条件构造器
            QueryWrapper<Employee> qw = new QueryWrapper<>();
            qw.eq("account", username);
            return employeeMapper.selectOne(qw);
        }
        return null;
    }

    @Override
    public boolean delEmployeeById(Integer employeeId) {
        int i = employeeMapper.deleteById(employeeId);
        if (i <= 0) {
            throw new ServiceException("服务器异常!");
        }
        return true;
    }

    @Override
    public boolean addEmployee(Employee employee) {
        if ("".equals(employee.getName())
                || "".equals(employee.getPassword())) {
            throw new ServiceException("姓名不能为空");
        }

        Employee userByUserName = this.getUserByUserName(employee.getName());
        if (userByUserName != null) {
            throw new ServiceException("姓名不能为空");
        }

        return employeeMapper.insert(employee) > 0;
    }

    @Override
    public boolean delEmployeeByIds(Integer[] ids) {
        if (ids == null || ids.length <= 0)
            throw new ServiceException("请传入id");
        List<Integer> list = Arrays.asList(ids);
        return employeeMapper.deleteBatchIds(list) > 0;
    }

    public Page<Employee> selectPage(Page<Employee> page) {
        return employeeMapper.selectPage(page, null);

    }

}
